<template>
	<view>
		<web-view :webview-styles="webviewStyles" :src="webUrl" v-if="webUrl"></web-view>
	</view>
</template> 
<script>
	/**
	 * 文件在线预览页面
	 * */
	var currentWebview
	export default {
		data() {
			return {
				webUrl:"",
				webviewStyles: {
					progress: {
						color: this.$mainColor
					}
				}
			}
		},
		onLoad(params) {
			let type = params['type']
			if(type) {
				if(!uni.config['docPath']) return this._error("docPath配置错误",2000)
				if(type=="xls"||type=="xlsx"){
					this._loadExcel(params['downurl'])
				}else{
					this._loadDoc(params['downurl'])
				}
			}
			this._setCalable()
		},
		methods:{
			_loadExcel(surl){
				if(uni.sutils.isH5()){
					surl = encodeURIComponent(surl)
				}
				var url = uni.config['docPath'] + "/x/_layouts/mobile/mXL.aspx?ui=zh-CN&rs=zh-CN&WOPISrc="
				var url1 = encodeURIComponent(uni.config['docPath'] + "/oh/wopi/files/@/wFileId?wFileId=" + surl)
				this.webUrl = url + url1 +"&access_token=0&access_token_ttl=1&wdMobileHost=3"
			},
			_loadDoc(surl){
				if(!surl) return this._error("预览地址错误",2000)
				var url = uni.config['docPath'] + '/op/embed.aspx?src='
				if(uni.sutils.isH5()){
					surl = encodeURIComponent(surl)
				}
				this.webUrl = url + surl
				console.log(surl)
			},
			_error(message,time){
				uni.showToast({
					title: message,
					icon: 'none'
				})
				setTimeout(()=>{
					uni.navigateBack()
				},time)
			},
			//设置可双指缩放
			_setCalable(){
				if(uni.sutil.isApp()){
					currentWebview = this.$mp.page.$getAppWebview()
					setTimeout(()=> {
					    currentWebview = currentWebview.children()[0]
					    currentWebview.setStyle({scalable:true})
					}, 500); //如果是页面初始化调用时，需要延时一下
				}
			}
		}
	}
</script>

<style lang="scss">

</style>
